var main_apis = {
	postCount: "http://api.fangchepingtai.com/fangchebbs/post/count",
	postListByPage: "http://api.fangchepingtai.com/fangchebbs/post/listbypage",
};
(function() {
	var banner_dtd = new $.Deferred();
	var page, num, pages;
	// 获取官网的动态栏
	$.when(function() {
		coAjax.get(public_api.banner.get.all, banner => {
			App.set("$Cache.main.banner.list", banner);
			// console.log(banner.info);
			banner_dtd.resolve(banner);
		}, err => {
			console.log(err);
		});
		return banner_dtd;
	}()).then(data => {
		var len = data.length;
		var box = $(".imgbox");
		var _init = 0;
		// banner轮播器
		setInterval(function() {
			var str = "translateX(-" + 100 * (_init % len) + "%)";
			box.css({
				"transform": str,
				"-webkit-transform": str
			});
			_init++;
		}, 8000);
	});
	// 发布新帖
	App.set("$Event.main.release_new_post", function() {
		if (!App.get("loginer")) {
			myConfirm("您还未登陆，请先登陆再执行操作", function() {
				Path.jump("/login.html");
			});
			return;
		};
		Path.jump("/post.html");
	});

	// 根据分页信息获取话题列表
	function getPostList(page, num) {
		coAjax.get(main_apis.postListByPage, {
			page: page,
			num: num
		}, function(result) {
			console.log(result);
			App.set("$Cache.main.post_list", result);
			document.body.scrollTop = 0;
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
	};

	// 翻页
	App.set("$Event.main.prePostPage", function() {
		var jumpPage = App.get("$Cache.main.current_post_page");
		jumpPage > 0 ? jumpPage-- : jumpPage = 0;
		Path.jump("/main.html?page=" + jumpPage + "&num=" + num);
	});
	App.set("$Event.main.nextPostPage", function() {
		var jumpPage = App.get("$Cache.mian.current_post_page");
		jumpPage < pages.length - 1 ? jumpPage++ : jumpPage = pages.length - 1;
		Path.jump("/main.html?page=" + jumpPage + "&num=" + num);
	});

	Path.on(["/__basename__.html", ""], function(_current_location) {
		var qs = _current_location.query;
		page = qs.get("page") || 0;
		App.set("$Cache.main.current_post_page", page);
		num = qs.get("num") || 15;
		App.set("$Cache.main.page_post_num", num);
		getPostList(page, num);
		// 获取分页信息
		coAjax.get(main_apis.postCount, function(result) {
			pages = [];
			pages.length = Math.ceil(result / num);
			App.set("$Cache.main.pages", pages);
			// console.log(result);
		}, function(err, errMsg, errCode) {
			console.log(err);
			console.log(errMsg);
		});
	});
}());